package com.seal.commons.copiers.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import static com.seal.commons.copiers.util.Preconditions.checkNotNull;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toCollection;

/**
 * 功能描述: <br/>
 *
 * @author huangyong
 * @date: 2020-05-31 21:08<br/>
 * @since JDK 1.8
 */
public interface ListCopier<S, T> extends BeanCopier<S, T>{


    default List<T> copyList(List<S> sourceList) {
        return copyList(sourceList, ArrayList::new);
    }


    /**
     * 将 sourceList 拷贝到新集合，使用自定义 List
     *
     * @param sourceList  源对象集合
     * @param listFactory 目标对象集合工厂，用于自定义收集器类型
     * @return 目标对象集合
     */
    default List<T> copyList(List<S> sourceList, Supplier<List<T>> listFactory) {
        checkNotNull(listFactory, "list factory cannot be null!");
        return Optional.ofNullable(sourceList)
                .filter(list -> !list.isEmpty())
                .map(list -> list.stream().map(this::copy).collect(toCollection(listFactory)))
                .orElse(emptyList());
    }


}